package com.itbaizhan;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.Callable;

/**jdbc工具类*/
/*在工具类的方法都是静态方法*/
public class JdbcUtils02 {
   private static String  url;
   private static String name;
   private static String pwd;
    static {
        try {
            Properties properties = new Properties();
            //获取将文件信息读进来的输入流
            InputStream is = JdbcUtils02.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //读取properties文件并解析
            properties.load(is);
           url =  properties.getProperty("url");
           name = properties.getProperty("name");
           pwd = properties.getProperty("pwd");
           String driver = properties.getProperty("driver");

            Class.forName(driver);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //获取数据库连接对象
    //要有返回值，Connection对象
    /*不可以在getConnection方法里放properties文件，因为读取文件时
     * 要iO流，那样每次调用getConnection方法时，都要启动IO流，频繁操作IO
     * 流对系统耗时，而且获取properties信息，只获取一次就可以了*/
    public static Connection getConnection(){
        Connection connection = null;
        try {
             connection = DriverManager.getConnection(url,name,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /*关闭Connection对象*/
    public static void closeConnection(Connection connection){
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /*提交事务*/
    public static void commit(Connection connection){
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /*事务回滚*/
    public static void rollback(Connection connection){
        try {
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //关闭statement对象
    //因为PreparedStatement接口是Statement的子接口，所以不用再写多一
    //个方法关闭PreparedStatement
    public static void closeStatement(Statement statement){
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //关闭ResultSet
    public static void closeResultSet(ResultSet resultSet){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //DML操作时关闭资源
    public static void closeResource(Statement statement,Connection connection){
        //先关闭Statement对象，再关闭Connection
        closeStatement(statement);
        closeConnection(connection);
    }

    //查询时关闭资源,方法可以重载，所以可以同名
    public static void closeResource(ResultSet resultSet,Statement statement,Connection connection){
        //后用先关
        //先关闭ResultSet
        closeResultSet(resultSet);
        //在闭Statement对象
        closeStatement(statement);
        //最后关闭Connection对象
        closeConnection(connection);
    }
}
